package JavaEE.NetWork6;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class HttpClient {
    private Socket socket = null;
    public HttpClient(String ip,int port) throws IOException {
        socket = new Socket(ip,port);
    }
    public void get(String url) throws IOException{
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()){
            String firstLine = "GET:" + url + " HTTP/1.1\n";
            String headers = "HOST:" + socket.getInetAddress().getHostAddress() + ":" + socket.getPort();
            String blankLine = "\n";
            String httpRequest = firstLine + headers + blankLine;

            outputStream.write(httpRequest.getBytes());
            outputStream.flush();

            byte[] bytes = new byte[1024 * 1024];
            int n = inputStream.read(bytes);
            String httpResponse = new String(bytes,0,n);
            System.out.println(httpResponse);
        }
    }
}
